package com.ljph.jfinal.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintStream;
import java.util.Locale;

/**
 * Created by yuzhou on 2017/4/7.
 */
public class PrintStreamLog extends PrintStream {

  private final static Logger _log = LoggerFactory.getLogger(System.out.getClass());
  private final boolean error;

  public PrintStreamLog(boolean error) {
    super(System.out);
    this.error = error;
  }

  @Override
  public void print(String s) {
    if(error) {
      _log.error(s);
    } else {
      _log.info(s);
    }
  }

  @Override
  public void println() {
    // super.println();
  }

  @Override
  public void println(String x) {
    if(error) {
      _log.error(x);
    } else {
      _log.info(x);
    }
  }

  @Override
  public PrintStream printf(String format, Object... args) {
    if(error) {
      _log.error(String.format(format, args));
    } else {
      _log.info(String.format(format, args));
    }
    return this;
  }

  @Override
  public PrintStream printf(Locale l, String format, Object... args) {
    if(error) {
      _log.error(String.format(l, format, args));
    } else {
      _log.info(String.format(l, format, args));
    }
    return this;
  }
}
